package com.edu.zju.scs.panacea.mapper.exposure;

import com.edu.zju.scs.panacea.entity.exposure.Exposure;
import com.edu.zju.scs.panacea.entity.query.ExposureQuery;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2019-02-08 13:26
 */
@Repository
public interface ExposureMapper {

    @Select("SELECT * FROM plan_exposure_total WHERE plan_id = #{planId}")
    @Results(id="exposureMap", value={
            @Result(property="planId",column="plan_id"),
            @Result(property="ageANum",column="age_a_num"),
            @Result(property="ageBNum",column="age_b_num"),
            @Result(property="ageCNum",column="age_c_num"),
            @Result(property="ageDNum",column="age_d_num"),
            @Result(property="ageENum",column="age_e_num"),
            @Result(property="marriageANum",column="marriage_a_num"),
            @Result(property="marriageBNum",column="marriage_b_num"),
            @Result(property="sexANum",column="sex_a_num"),
            @Result(property="sexBNum",column="sex_b_num"),
            @Result(property="totalAmount",column="total_amount")})
    Exposure selectExposureTotal(Long planId);


    @Select("<script> " +
            "SELECT * from plan_exposure_date where 1 = 1 "+
            " <if test='planId != null'> and plan_id = #{planId} </if> "+
            " <if test='startDate != null'> and exposure_date between #{startDate} and #{endDate} </if> "+
            " ORDER BY exposure_date ASC" +
            " </script>")
    @Results(id="exposureDateMap", value={
            @Result(property="planId",column="plan_id"),
            @Result(property="exposureDate",column="exposure_date"),
            @Result(property="todayAmount",column="today_amount"),
            @Result(property="totalAmount",column="total_amount")})
    List<Exposure> selectExposureDate(ExposureQuery exposureQuery);

    @Insert("INSERT INTO plan_exposure_date(plan_id, exposure_date, today_amount, total_amount) VALUES (#{planId}, #{exposureDate}, #{todayAmount}, #{totalAmount})")
    long insertExposureDate(Exposure exposure);

    @Insert("INSERT INTO plan_exposure_total(plan_id, sex_a_num, sex_b_num, age_a_num, age_b_num, age_c_num, age_d_num, age_e_num, marriage_a_num, marriage_b_num, total_amount) VALUES (" +
            "#{planId}, #{sexANum}, #{sexBNum}, #{ageANum}, #{ageBNum}, #{ageCNum}, #{ageDNum}, #{ageENum}, #{marriageANum}, #{marriageBNum}, #{todayAmount})")
    long insertExposureTotal(Exposure exposure);


    @Update({ "update plan_exposure_total set " +
            "sex_a_num = sex_a_num + #{sexANum}, " +
            "sex_b_num = sex_b_num + #{sexBNum}, " +
            "age_a_num = age_a_num + #{ageANum}, " +
            "age_b_num = age_b_num + #{ageBNum}, " +
            "age_c_num = age_c_num + #{ageCNum}, " +
            "age_d_num = age_d_num + #{ageDNum}, " +
            "age_e_num = age_e_num+ #{ageENum}, " +
            "marriage_a_num = marriage_a_num + #{marriageANum}, " +
            "marriage_b_num = marriage_b_num + #{marriageBNum}, " +
            "total_amount = total_amount + #{todayAmount} " +
            "where plan_id = #{planId}" })
    long updateExposureTotal(Exposure exposure);
}
